<?php

namespace App\Http\Controllers;


use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Tools\Func;
use Illuminate\Support\Facades\DB;

use App\Models\HeTong;

class ChaController extends Controller
{
    use Func; 

    public function cha1()
    {
        return view('cha1');
    }   
	
	public function cha2()
	{
	    return view('cha2');
	}   

    public function cha3(Request $request)
    {
	    $id = $request->id;
	    return view('cha3', ['id' => $id]);
    }   
	
	public function cha4(Request $request)
	{
	    $id = $request->id;
	    return view('cha4', ['id' => $id]);
	}   
	
	public function cha5(Request $request)
	{
	    $id = $request->id;
	    if ($res = HeTong::find($id)) {
	        $no = $res->no;
	        $res->sfz = substr($no, 0, 4) . '***********'.substr($no, strlen($no)-3, 3);
	    }
	    // dd($res);
	    return view('cha5', ['data' => $res]);
	}  
	
	public function cha6(Request $request)
	{
	    $id = $request->id;
	    $res = HeTong::find($id);
	    return view('cha6', ['data' => $res, 'yongtu' => $request->yongtu]);
	}  

    public function heTongView3(Request $request)
    {
        $name = $request->name;
        $no = $request->no;
        $diqu = $request->diqu;
        $res = HeTong::where([
                'no' => $no,
                'name' => $name,
                'diqu' => $diqu
            ])->get();
        // echo count($res);exit;
        if (!count($res)){
            return $this->returnJson(1, '没找到数据');   
        }

        return $this->returnJson(0, 'success', $res);
    }   

    public function getHeTong(Request $request)
    {
        if (!$username = trim($request->username) ?? '') {
            return $this->returnJson(1, '账号不能为空', $request->all());    
        }
            if (!$password = trim($request->password) ?? '') {
            return $this->returnJson(1, '密码不能为空', $request->all());    
        }
        if (!$resHeTong= HeTong::where([
                'no' => $username
            ])->first()) {
            return $this->returnJson(1, '账号密码错误', $request->all());  
        }

        return $this->returnJson(0, 'success', $resHeTong);
    }

}
